/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife updater.

    Ages of Strife updater is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife updater is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife updater.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.sql.SQLException;

public class Utils {
	public static int startTravel( java.sql.ResultSet resultSet, java.sql.Statement statement, int army_id, int x, int y, int target_x, int target_y, long turn, String date ) throws SQLException {
		int value = -1;
		resultSet = statement.executeQuery( "SELECT ruler_id, follow " +
				"FROM army " +
				"WHERE army_id = " + army_id );
		if( resultSet.next() ) {
			int ruler_id = resultSet.getInt( "ruler_id" );
			int follow = resultSet.getInt( "follow" );
			resultSet = statement.executeQuery( "SELECT SUM( scout + infantry + cavalry + artillery ) " +
					"FROM army " +
					"WHERE army_id = " + army_id );
			resultSet.next();
			if( resultSet.getInt( 1 ) > 0 && 
				!( x == target_x && y == target_y ) ) {
				resultSet = statement.executeQuery( "SELECT max(x), MAX(y) " +
						"FROM village" );
				if( resultSet.next() && resultSet.getInt( 1 ) >= target_x && resultSet.getInt( 2 ) >= target_y &&
						target_x > -1 && target_y > -1 ) { //the move order seems to be valid
					double travel = Math.sqrt( ( ( target_x - x ) * ( target_x - x ) + ( target_y - y ) * ( target_y - y ) ) );
					//double distance = travel;
					resultSet = statement.executeQuery( "SELECT ruler_id, name, village_id, add_turn " +
							"FROM village " +
							"WHERE x = " + target_x + " AND y = " + target_y );
					if( resultSet.next() ) {
						//int attacked_faction = -1;
						int village_id = resultSet.getInt( "village_id" );
						int village_ruler_id = resultSet.getInt( "ruler_id" );
						int add_turn = resultSet.getInt( "add_turn" );
						
						/* New system to protect newbies,
						 * You can not move to villages which have been added less then 800 turns ago. 
						 * Unless you have started less then 200 turns before the village was added
						 */
						boolean no_bash = true;
						if( add_turn + 800 > turn ) { // the village has been added less then 800 turns ago, we need to check if the ruler has started about the same turn or later
							resultSet = statement.executeQuery( "SELECT start_turn FROM ruler WHERE ruler_id = " + ruler_id );
							if( resultSet.next() && resultSet.getInt( "start_turn" ) + 200 < add_turn ) {
								AgesOfStrifeServer.log.debug( "Attacking ruler started much earlier! So no going there :)" );
								no_bash = false;
							}
						}
						
						if( no_bash ) {
							if( ruler_id == village_ruler_id && follow == -1 ) { 
								// move to own village and not following an army
								resultSet = statement.executeQuery( "SELECT count(*) " +
										"FROM army " +
										"WHERE follow = " + army_id );
								if( resultSet.next() && resultSet.getInt( 1 ) == 0 ) { 
									// not being followed by an army
									resultSet = statement.executeQuery( "SELECT count(*) " +
											"FROM village_buildings " +
											"WHERE village_id = " + village_id + " AND building_id = 13" );
									if( resultSet.next() && resultSet.getInt( 1 ) > 0 ) { 
										// speed bonus for the army
										travel *= 0.75;
									}
								}
							}
						}
						else {
							travel = -1;
						}
					}
					value = ( int ) Math.round( travel );
				}
			}
			else if( x == target_x && y == target_y ) {
				value = 0;
			}
		}
		
		return value;
	}
	
	public static void notifyOfHistory(java.sql.ResultSet resultSet, java.sql.Statement statement, int history_id ) throws SQLException {
		resultSet = statement.executeQuery( "SELECT count(ruler_id) " +
	    		"FROM users " +
	    		"JOIN ruler ON users.user_id = ruler.user_id " +
	    		"WHERE highlight_history = 1" );
		resultSet.next();
		int total = resultSet.getInt( 1 );
		resultSet = statement.executeQuery( "SELECT ruler_id " +
	    		"FROM users " +
	    		"JOIN ruler ON users.user_id = ruler.user_id " +
	    		"WHERE highlight_history = 1" );
		
		int[] ruler_ids = new int[total];
		int i = 0;
		
		while ( resultSet.next() ) {
			ruler_ids[i] = resultSet.getInt( "ruler_id" );
			i ++;
		}
		
		for( int p = 0; p < total; p ++ ) {
			statement.execute( "INSERT INTO history_seen " +
					"( ruler_id, history_id ) " +
					"VALUES ( " + ruler_ids[p] + ", " + history_id + ")" );
		}
	}
	
	public static String formatNumber( String number ) {
		if( number != null && number.length() > 0 ) {
			String ret = "";
			for( int i = 0; i < number.length(); i ++ ) {
				if( i % 3 == 0 && i != 0 ) {
					ret = "," + ret;
				}
				ret = number.substring( ( number.length() - i - 1 ), ( number.length() - i ) ) + ret;
			}
			
			return ret;
		}
		else {
			return "0";
		}
	}
}
